﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace XML_Parser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            string test = openFileDialog1.FileName;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(test);
            XmlNode oRootNode = xmlDoc.DocumentElement;
            XmlNode WeightNode = oRootNode.SelectSingleNode("//Weight");
            if (WeightNode != null)
            {
                double MaxWeight = 0;
                DateTime EarliestMeasurement = DateTime.Now;
                DateTime LastMeasurement = DateTime.Now;
                foreach (XmlNode MeasurementNode in WeightNode.ChildNodes)
                {
                    double stones = Convert.ToDouble(MeasurementNode.Attributes["Stones"].Value);
                    double pounds =  (Convert.ToDouble(MeasurementNode.Attributes["Pounds"].Value) / 14);
                    double weight = (stones + pounds);
                    if (weight > MaxWeight)
                    {
                        MaxWeight = weight;
                    }
                    DateTime MeasurementDate = Convert.ToDateTime(MeasurementNode.Attributes["DateTime"].Value.ToString());
                    if (EarliestMeasurement.Date > MeasurementDate.Date)
                    { 
                        EarliestMeasurement = MeasurementDate;
                    }
                    if (LastMeasurement.Date < MeasurementDate.Date)
                    {
                        LastMeasurement = MeasurementDate;
                    }
                }
                while (EarliestMeasurement.Date != DateTime.Now.Date)
                {
                    bool hascurrentdate = false;
                    foreach (XmlNode MeasurementNode in WeightNode.ChildNodes)
                    {
                        DateTime MeasurementDate = Convert.ToDateTime(MeasurementNode.Attributes["DateTime"].Value.ToString());
                        if (EarliestMeasurement == MeasurementDate)
                        {
                            hascurrentdate = true;
                        }
                    }
                    if (!hascurrentdate)
                    {
                        XmlNode NewNode = xmlDoc.CreateNode(XmlNodeType.Element, "Measurement", "");
                        XmlAttribute xmlatt = xmlDoc.CreateAttribute("Stones");
                        NewNode.Attributes.Append(xmlatt);
                        xmlatt = xmlDoc.CreateAttribute("Pounds");
                        NewNode.Attributes.Append(xmlatt);
                        xmlatt = xmlDoc.CreateAttribute("DateTime");
                        NewNode.Attributes.Append(xmlatt);
                        NewNode.Attributes["DateTime"].Value = EarliestMeasurement.Date.ToString();
                        NewNode.Attributes["Stones"].Value = "0";
                        NewNode.Attributes["Pounds"].Value = "0";
                        WeightNode.AppendChild(NewNode);
                    }
                    EarliestMeasurement = EarliestMeasurement.AddDays(1);
                }
                XmlNode MaxWeightNode = WeightNode.SelectSingleNode("//MaxWeight");
                if (MaxWeightNode == null)
                {
                    MaxWeightNode = xmlDoc.CreateNode(XmlNodeType.Element, "MaxWeight", "");
                    XmlAttribute xmlatt = xmlDoc.CreateAttribute("Stones");
                    MaxWeightNode.Attributes.Append(xmlatt);
                    WeightNode.AppendChild(MaxWeightNode);
                }
                MaxWeightNode.Attributes["Stones"].Value = MaxWeight.ToString();
            }
            xmlDoc.Save(test);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }
    }
}
